﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjetoJornada_Negocios.Fachada;
using Basicas;

namespace ProjetoJornada.Interno
{
    public partial class Matricula_ListarPorTurma : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (Session["Jornada"] != null)
                {
                    List<Turma> turmas = Fachada.Instance.ConsultarTodasAsTurmasPorJornada(Convert.ToInt32(Session["Jornada"]));

                    ddlTurmas.Items.Add("");

                    foreach (Turma turma in turmas)
                    {
                        ListItem item = new ListItem();
                        item.Text = turma.Nome;
                        item.Value = turma.Id.ToString();
                        this.ddlTurmas.Items.Add(item);
                    }
                }
                else
                {
                    Response.Redirect("Jornada_Escolher.aspx?urlReturn=" + HttpContext.Current.Request.Url.AbsolutePath);
                }
                
            }
        }

        protected void ddlTurmas_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlTurmas.SelectedIndex != 0)
            {
                int turmaId = Convert.ToInt32(this.ddlTurmas.SelectedValue);
                Turma turma = new Turma();
                turma.Id = turmaId;

                ViewState["Turma"] = Fachada.Instance.ConsultarTurmaPorId(turma.Id);
           
                List<Matricula> matriculas = Fachada.Instance.BuscarMatriculaPorTurma(turma);

                if (matriculas.Count == 0)
                {
                    this.lblMensagem.Text = "Nenhuma matrícula encontrada.";
                }
                else
                {
                    Session["Matriculas"] = matriculas;

                    this.GridView_Matricula.DataSource = matriculas;
                    this.GridView_Matricula.DataBind();
                }
            }
         
        }

          protected void GridView_Matricula_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            List<Matricula> matriculas = (List<Matricula>)Session["Matriculas"];
            Matricula matricula = matriculas[e.RowIndex];
            Fachada.Instance.RemoverMatricula(matricula);
            matriculas.Remove(matricula);

            this.GridView_Matricula.DataSource = matriculas;
            this.GridView_Matricula.DataBind();
        }

          protected void GridView_Matricula_RowDataBound(object sender, GridViewRowEventArgs e)
          {
              if (e.Row.DataItem is Matricula)
              {
                  Matricula matricula = e.Row.DataItem as Matricula;
                  Label cpf = e.Row.FindControl("lblCpf") as Label;
                  cpf.Text = matricula.Aluno.Cpf;

                  Label nome = e.Row.FindControl("lblNome") as Label;
                  nome.Text = matricula.Aluno.Nome;

                  Label data = e.Row.FindControl("lblData") as Label;
                  data.Text = matricula.DataMatricula.ToString("dd/MM/yyyy");

                  Label status = e.Row.FindControl("lblStatus") as Label;
                  if (matricula.StatusPagamento)
                  {
                      status.Text = "Pago";
                  }
                  else
                  {
                      status.Text = "Em aberto.";
                  }

                  Turma turma = ViewState["Turma"] as Turma;
                  Label curso = e.Row.FindControl("lblCurso") as Label;
                  curso.Text = turma.Curso.Nome;
              }
         }

       
    }
}
